﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using CollegeWebsite.Models;

namespace CollegeWebsite.Controllers
{
    public class StudentController : Controller
    {
    private CollegeDataContext _dataContext = new CollegeDataContext();


       public ActionResult Login()
       {
           if (Session["LoginStudent"]!= null)
           {
               var std = (Student)Session["LoginStudent"];
               return View("LoggedIn",std);
           }
         
             
           return View();
          
         
        
       }
        
        
        [HttpPost]
        public ActionResult Login(Student std)
        {
            if (std.Email == "" || std.Password == "") return View(std);

            Thread.Sleep(2000);
            var logonStudent =
                _dataContext.Students.Select(s => s).Where(s => (s.Email == std.Email && s.Password == std.Password)).
                    SingleOrDefault();
            if(logonStudent != null)
            {
                Session["LoginStudent"] = logonStudent;
                return View("LoggedIn", logonStudent);
            }
           return View();
        }


        public ActionResult LogOut()
        {
           Session.Remove("LoginStudent");
           return RedirectToAction("Index", "Home");
        }
       
        [HttpPost]
        public ActionResult ApplyAdmission(Student stu,FormCollection formCollection, Course crs)
        {
            Common com = new Common();
            Session["captcha"] = com.CreateCaptcha("5");

            Student newStu = stu;
            return View();
        }

       
    }
}
